package com.gxa.controller.parking;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.parking.PSpaceSearchDto;
import com.gxa.dto.parking.ParkingSpacesDto;
import com.gxa.entity.parking.PRegion;
import com.gxa.entity.parking.Parking;
import com.gxa.entity.parking.ParkingSpaces;
import com.gxa.service.parking.ParkingSpacesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Api(tags = "车位列表")
@RestController
@RequestMapping("/parking-spaces")
public class ParkingSpacesController {
    @Autowired
    private ParkingSpacesService parkingSpacesService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("展示停车场")
    @PostMapping("/list")
    public Result selectAll(@RequestBody PSpaceSearchDto pSpaceSearchDto,Integer pageNum,Integer pageSize) {
        PageInfo<ParkingSpacesDto> parkingSpacesDtoPageInfo;
        try {
            parkingSpacesDtoPageInfo = parkingSpacesService.selectAll(pSpaceSearchDto, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.success(parkingSpacesDtoPageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("新增停车场")
    @PostMapping("/add")
    public Result add(@RequestBody ParkingSpaces parkingSpaces) {
        parkingSpacesService.add(parkingSpaces);
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除停车场")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id) {
        parkingSpacesService.deleteById(id);
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据id查询停车场接口")
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable("id") Integer id) {
        ParkingSpacesDto parkingSpacesDto = parkingSpacesService.selectByid(id);
        return Result.success(parkingSpacesDto);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("编辑停车场")
    @PutMapping("/edit")
    public Result edit(@RequestBody ParkingSpaces parkingSpaces) {
        parkingSpacesService.update(parkingSpaces);
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("查询所有停车场的接口")
    @GetMapping("/queryParking")
    public Result queryParking() {
        List<Parking> parkings = parkingSpacesService.queryParking();
        return Result.success(parkings);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据停车场的编号查询所有区域的接口")
    @GetMapping("/queryRegionByParkingNum")
    public Result queryRegionByParkingNum( String parkingNum) {
        List<PRegion> pRegions = parkingSpacesService.queryPRegion(parkingNum);
        return Result.success(pRegions);
    }
}
